<script lang="ts" setup>
import { ref, reactive, watch } from "vue"
import { type FormInstance } from "element-plus"
import { Search, Refresh } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
import { getOrderDataApi } from "@/api/operationLog"
import { formatDateTime } from "@/utils"
import { shortcuts } from "@/utils/dateSet"

defineOptions({
  name: "LogOrder"
})
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
// #region 查
const defaultTime1: [Date, Date] = [new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 0)]
const searchFormRef = ref<FormInstance | null>(null)
const loading = ref<boolean>(false)
// 搜索内容
const searchData = reactive<any>({
  time: "",
  massage: "",
  order_id: ""
})
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  if (paginationData.currentPage === 1) {
    getTableData()
    console.log("重置")
  }
  paginationData.currentPage = 1
}
const handleSearch = () => {
  if (paginationData.currentPage === 1) {
    getTableData()
    console.log("查询")
  }
  paginationData.currentPage = 1
}
// #endregion

//  #region 查看订单日志
// const tableData = ref<any>([])
const tableData = ref()
const getTableData = async () => {
  if (!searchData.time) {
    searchData.time = []
  }
  loading.value = true
  await getOrderDataApi({
    start: new Date(searchData.time[0]).getTime() || "",
    end: new Date(searchData.time[1]).getTime() || "",
    order_id: searchData.order_id || "",
    massage: searchData.massage || "",
    page: paginationData.currentPage,
    size: paginationData.pageSize
  })
    .then((res) => {
      paginationData.total = res.data.total
      tableData.value = res.data.date
      console.log(res, "获取所有订单日志")
      loading.value = false
    })
    .finally(() => {
      loading.value = false
    })
}

/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })

// #endregion
</script>

<template>
  <div class="app-container">
    <el-card shadow="never" class="search-wrapper" v-loading="loading">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="order_id" label="订单ID">
          <el-input v-model.trim="searchData.order_id" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="massage" label="关键字查询" style="width: 380px">
          <el-input v-model.trim="searchData.massage" placeholder="请输入身份证号、手机号或其它关键字" />
        </el-form-item>
        <el-form-item prop="time" label="操作日期">
          <el-date-picker
            v-model="searchData.time"
            type="datetimerange"
            range-separator="到"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            :shortcuts="shortcuts"
            :default-time="defaultTime1"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card" v-loading="loading">
      <el-scrollbar height="600px">
        <el-timeline>
          <el-timeline-item
            v-for="(item, index) in tableData"
            :key="index"
            :timestamp="formatDateTime(item.update_time)"
          >
            <p>
              <el-text class="mx-1" size="large" type="success">序号:</el-text>
              <el-text class="mx-1" type="success">
                {{ (paginationData.currentPage - 1) * paginationData.pageSize + index + 1 }}</el-text
              >
            </p>
            <div style="margin-top: 15px">
              <el-text class="mx-1" size="large">操作人:</el-text>
              <el-text class="mx-1" type="danger"> {{ item.operation_name }}</el-text>
            </div>
            <p>
              <el-text class="mx-1" size="large">订单号:</el-text>
              <el-text class="mx-1" type="danger"> {{ item.order_id }}</el-text>
            </p>
            <p>
              <el-text class="mx-1" size="large">状态:</el-text>
              <el-text class="mx-1" type="danger">{{ item.operation_status }}</el-text>
            </p>
            <p v-if="item.message !== null"><span v-html="item.message.replace(/\\n/gm, '<br>')" /><br /></p>
          </el-timeline-item>
        </el-timeline>
      </el-scrollbar>

      <!-- 分页器 -->
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
}
.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
.box-card {
  overflow-wrap: break-word;
}
</style>
